package com.czk.netty.zerocopy;

import java.io.FileInputStream;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.channels.FileChannel;
import java.nio.channels.SocketChannel;

/**
 * @Author:ChenZhangKun
 * @Date: 2021/1/12 22:25
 */
public class NewIoClient {
    public static void main(String[] args) throws IOException {
        SocketChannel socketChannel = SocketChannel.open();
        socketChannel.connect(new InetSocketAddress("localhost", 7001));
        String fileName = "d:\\简历\\x.txt";
        // 得到channel
        FileChannel fileChannel = new FileInputStream(fileName).getChannel();
        // 准备发送
        long start = System.currentTimeMillis();
        // 在linux下，一个transferTo方法就可以完成传输
        // 在windows下，一个transferTo只能发送8m,大于8m需要分段传输
        // transferTo方法就实现了零拷贝，没有cpu拷贝
        long l = fileChannel.transferTo(0, fileChannel.size(), socketChannel);
        System.out.println("发送总的字节数=" + l + "耗时" + (System.currentTimeMillis() - start));
    }
}
